package org.hackystat.sdsa.analyzer.model.activity.edit;


import java.util.logging.Logger;

import jess.Fact;
import jess.JessException;
import jess.RU;
import jess.Rete;
import jess.Value;

import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Implements edit activity on documentation file.
 * 
 * @author Hongbing Kou
 */
public class DocumentEditActivity extends EditActivity {
  /** DevEvent type. */
  private static final String DOCUMENT_EDIT_TYPE = "DOCUMENT EDIT";
  
  /**
   * Empty constructor.
   */
  public DocumentEditActivity() {
    super.setType(DOCUMENT_EDIT_TYPE);
  }
  
  /**
   * Instantiates document edit action. 
   * 
   * @param sensorData Sensor data.
   * @param logger Logger.
   */
  public DocumentEditActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    super.setType(DOCUMENT_EDIT_TYPE);
}
  
  /**
   * Gets production code edit action string.
   * 
   * @return Edit action string. 
   */
  @Override public String toString() {
    StringBuffer buf = new StringBuffer(30);
    
    buf.append(super.toString());
    buf.append(" {");
    buf.append(makeMetricPair("FI", this.fileSizeIncrease, this.fileSize));
    buf.append('}');

    return  buf.toString();
  }
  
  /**
   * Makes Jess facts in the given Jess rete engine.
   * 
   * @param index Action index in episode. 
   * @param engine Jess rete engine.
   * @throws JessException If error while constructing jess action.
   * @return The asserted fact.
   */
  @Override public Fact assertActivity(int index, Rete engine) throws JessException  {
    Fact assertedFact = null;
    if (isSubstantial()) {
      Fact f = new Fact("DocumentEditActivity", engine);
      f.setSlotValue(INDEX_SLOT, new Value(index, RU.INTEGER));
      String file = super.extractFileName(super.getResourceUri());
      f.setSlotValue(FILE_SLOT, new Value(file, RU.STRING));
      f.setSlotValue("duration", new Value(this.getDuration(), RU.INTEGER));
      assertedFact = engine.assertFact(f);
    }
    
    return assertedFact;
  }
  
  /**
   * Checks documentation edit work.
   * 
   * @return True if edit time is positive.
   */
  @Override public boolean hasProgress() {
    return this.getDuration() > 0 || this.getFileSizeIncrease() != 0;
  }

  /**
   * Checks whether this edit work is significant or not.
   * 
   * @return True if there is substantial work and false otherwise.
   */
  @Override public boolean isSubstantial() {
    return this.getDuration() > 0 && this.getFileSizeIncrease() != 0;  
  }

  
  /** 
   * Get the representation of this activity. 
   * 
   * @return String representation of this activity.
   */
  @Override public String getRepresentation() {
    StringBuffer buf = new StringBuffer();
    buf.append(super.getDuration()).append("sec ");    
    buf.append(makeMetricPair("FI", this.fileSizeIncrease, this.fileSize));
    
    return buf.toString();
  }
}